package com.polaris.server.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.polaris.common.utils.RespBean;
import com.polaris.server.mapper.SysRoleMapper;
import com.polaris.server.mapper.SysUserMapper;
import com.polaris.server.mapper.SysUserRoleMapper;
import com.polaris.server.pojo.SysRole;
import com.polaris.server.pojo.SysUserRole;
import com.polaris.server.pojo.SysUser;
import com.polaris.server.dto.UserUpdateParam;
import com.polaris.server.service.IAdminService;
import com.polaris.server.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author polaris
 * @since 2021-05-19
 */
@Api(tags="系统用户管理")
@RestController
@RequestMapping("/api/user")
public class SysUserController {
    private final SysUserRoleMapper adminRoleRelationMapper;
    private final ISysUserService userService;
    private final IAdminService adminService;
    private final SysUserMapper userMapper;
    private final SysRoleMapper roleMapper;

    public SysUserController(SysUserRoleMapper adminRoleRelationMapper, ISysUserService userService, IAdminService adminService, SysUserMapper userMapper, SysRoleMapper roleMapper) {
        this.adminRoleRelationMapper = adminRoleRelationMapper;
        this.userService = userService;
        this.adminService = adminService;
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页查询用户list")
    public RespBean list(Principal principal, @RequestParam Integer page, @RequestParam Integer limit, @RequestBody SysUser param){
        SysUser admin = adminService.getAdminByUserName(principal.getName());
        Long roleId = adminRoleRelationMapper.selectOne(new QueryWrapper<SysUserRole>().eq("admin_id", admin.getUserId())).getRoleId();
        SysRole role = roleMapper.selectOne(new QueryWrapper<SysRole>().eq("roleId",roleId));

        if(admin.getIsAdmin()==1 || role.getLevel()==1){
            Page<SysUser> userPage = new Page<>(page, limit);
            JSONObject jsonObject = JSONUtil.parseObj(param);
            if(jsonObject.isEmpty()){
                return userService.getUserList(userPage);
            }
            return userService.getUserList(userPage, param);
        }
        return RespBean.error("无权进行该项查询操作");
    }

    @PostMapping("/update/phone")
    @ApiOperation(value = "修改用户手机号")
    public RespBean updatePhone(Principal principal, @RequestBody UserUpdateParam updateParam){
        SysUser oldUser = adminService.getAdminByUserName(principal.getName());
        oldUser = userService.updatePhone(oldUser, updateParam.getPhone());
        if(oldUser == null){
            return RespBean.error("修改用户手机号失败");
        }
        else{
            return RespBean.success("修改用户手机号成功",oldUser.getPhone());
        }
    }
}
